package com.lngsyz.eseal.base.biz.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lngsyz.eseal.base.entity.LoginInfo;
import com.lngsyz.eseal.base.entity.notLogin.CheckRandomCode;
import com.lngsyz.eseal.base.entity.notLogin.InFindPasswd;
import com.lngsyz.eseal.base.entity.notLogin.InGetRandomCode;
import com.lngsyz.eseal.base.entity.notLogin.InLogin;
import com.lngsyz.eseal.base.entity.notLogin.InRegist;
import com.lngsyz.eseal.base.entity.notLogin.OutGetRandomCode;
import com.lngsyz.eseal.base.entity.source1.TUser;
import com.lngsyz.eseal.base.entity.util.CaptchaEntity;
import com.lngsyz.eseal.base.exception.RequestSystemException;
import com.lngsyz.eseal.base.util.CaptchaGenerator;
import com.lngsyz.eseal.base.util.CommonUtil;
import com.lngsyz.eseal.base.util.SMSUtil;
import com.lngsyz.eseal.base.util.jwt.TokenUtil;

@Service
public class NotLoginService {
    
	@Autowired
	RedisService redisService;
	@Autowired
	TUserService tUserService;
	
	public final String PRE_TOKEN = "ESEAL_BASE_TOKEN_";

	
	public OutGetRandomCode getRandomCode(InGetRandomCode inGetRandomCode) throws Exception {
		OutGetRandomCode rtn = new OutGetRandomCode();
		CaptchaEntity ce = CaptchaGenerator.drawBase64();
		if(ce != null) {
			// 生成动态验证码
			String code = ce.getCode();
			// 图片base64
			String image = ce.getBase64Str();
			String codeType = inGetRandomCode.getCodeType();
			String key = redisService.setVerifyCode(code, codeType);
			rtn.setKey(key);
			rtn.setImage(image);
			return rtn;
		} else {
			throw new RequestSystemException("动态码生成失败");
		}
    }
	
	public void checkRandomCode(CheckRandomCode checkRandomCode) throws Exception {
		// 验证动态验证码
		String redisValue = redisService.getVerifyCode(checkRandomCode.getKey(), checkRandomCode.getCodeType());
		if(StringUtils.equalsIgnoreCase(redisValue, checkRandomCode.getGraphCode())) {
			String mobile = checkRandomCode.getAccount();
			// 通过的话，发送短信
			if(CommonUtil.isMobile(mobile)) {
				// 发送短信, 短信码
				// String code = SMSUtil.sendSMS(mobile);
				String code = "123456";
				redisService.setMobileCode(checkRandomCode.getAccount(), code, checkRandomCode.getCodeType());
			} else {
				throw new RequestSystemException("输入的账号应为11位手机号码");
			}
		} else {
			throw new RequestSystemException("验证失败");
		}
	}
	
	public String regist(InRegist inRegist) throws Exception {
		String redisSaveCode = redisService.getMobileCode(inRegist.getAccount(), "1");
		// String redisSaveCode = "123456";
		if(StringUtils.isNotBlank(redisSaveCode) && StringUtils.equals(redisSaveCode, inRegist.getCode())) {
			TUser tUser = new TUser();
			tUser.setId(CommonUtil.createUuid());
			tUser.setMobile(inRegist.getAccount());
			tUser.setPasswd(inRegist.getPasswd());
			tUser.setAdmin("0");
			tUser.setDistrictCity(inRegist.getDistrictCity());
			TUser tu = tUserService.insert(tUser);
			
			InLogin inLogin = new InLogin();
			inLogin.setAccount(tu.getMobile());
			inLogin.setPasswd(tu.getPasswd());
			return login(inLogin);
		} else {
			throw new RequestSystemException("短信验证失败");
		}
	}
	
	public String login(InLogin inLogin) throws Exception {
		boolean checkFlag = tUserService.checkPasswd(inLogin.getAccount(), inLogin.getPasswd());
		if(checkFlag) {
			TUser tUser = tUserService.findOneByMobile(inLogin.getAccount());
			LoginInfo loginInfo = new LoginInfo();
			loginInfo.setId(tUser.getId());
			loginInfo.setMobile(tUser.getMobile());
			loginInfo.setAdmin(tUser.getAdmin());
			// create jwt token
			String token = TokenUtil.generateToken(loginInfo);
			// 记录缓存
			redisService.refreshTemp(tUser.getId());
			return token;
		} else {
			throw new RequestSystemException("账号或密码错误");
		}
	}
	
	public void findPasswd(InFindPasswd inFindPasswd) throws Exception {
		String redisSaveCode = redisService.getMobileCode(inFindPasswd.getAccount(), "2");
		if(StringUtils.isNotBlank(redisSaveCode) && StringUtils.equals(redisSaveCode, inFindPasswd.getCode())) {
			tUserService.updatePasswd(inFindPasswd.getAccount(), inFindPasswd.getPasswd());
		} else {
			throw new RequestSystemException("短信验证失败");
		}
	}
	
	public LoginInfo decodeLoginInfoByToken(String token) {
    	LoginInfo loginInfo = TokenUtil.getUserInfoFromToken(token);
    	return loginInfo;
    }
	
	public String refreshToken(LoginInfo loginInfo) {
		String token = TokenUtil.generateToken(loginInfo);
		return token;
	}
}
